package com.lpy.book.controller;


import com.lpy.book.model.*;
import com.lpy.book.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;

@Slf4j
@RequestMapping("/book")
@ResponseBody
@Controller
public class BookController {

    @Autowired
    private BookService bookService;

//    添加图书
    @RequestMapping(value = "/addBook", produces = "application/json")
    public String addBook(BookInfo bookInfo){

        log.info("添加图书，bookinfo:{}", bookInfo);

        // 校验参数
        if(!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getCount() <= 0
                || bookInfo.getPrice() == null) {
            return "参数错误";
        }

        // 添加图书
        try {
            bookService.insertBook(bookInfo);
        }catch (Exception e) {
            log.error("添加图书失败, e:{}", e);
            return "内部发生错误，请联系管理员";
        }

        return "";
    }


//    显示列表
    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(PageRequest pageRequest) {
        log.info("查询列表信息，pageRequest:{}", pageRequest);

        if (pageRequest.getCurrentPage() < 1) {
            return Result.fail("非法参数");
        }

        PageResult<BookInfo> pageResult = bookService.getListByPage(pageRequest);

        return Result.success(pageResult);
    }


//     根据 id 查询图书
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {

        log.info("查询图书信息，bookId:{}", bookId);

        return bookService.queryBookById(bookId);
    }


//    更新图书 or 删除图书（逻辑删除）
    @RequestMapping("/updateBook")
    public Result updateBook(BookInfo bookInfo) {

        log.info("更新图书，updateBook:{}", bookInfo);

        // 校验参数
        if(bookInfo.getId() < 0) {
            return Result.fail(false,"ID 不合法");
        }

        try {
            // 更新
            Integer result = bookService.updateBook(bookInfo);

            // 失败
            if (result <= 0) {
                log.info("更新数据条数为0");
                return Result.fail(false,"更新数据为0");
            }
            return Result.success(true);
        }catch (Exception e) {
            log.error("更新图书失败, e:{}", e);
            return Result.fail(false, e.getMessage());
        }
    }


//    批量删除
    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> list) {
        log.info("批量删除数据, list:{}", list);

        try {
            Integer result = bookService.batchDelete(list);

            return result > 0;

        }catch (Exception e) {
            log.error("批量删除失败，id:{}, e:{}", list, e);
            return false;
        }

    }

}
